import time

import requests, json
import datetime
###########################

# actId = 2405240355774066  # 接龙的id
actId = 2405220059978897  # 接龙的id
# actId = 2405240355774066  # 接龙的id
actGoodId = 535102334
# actGoodId = 536006621
# actId = 2405240327639100  # 接龙的id

###########################
while datetime.datetime.now() < datetime.datetime(2024, 5, 24, 20, 0, 0):
# while datetime.datetime.now() < datetime.datetime(2024, 5, 24, 17, 16, 0):
    pass

goodsUrl = "https://apipro.qunjielong.com/activity-biz/activity/detail/goods"
data = {
  "actId": actId
}
headers = {
    "Host": "apipro.qunjielong.com",
    "Connection": "keep-alive",
    "device-type": "10",
    "mini-route": "pro/pages/generate-order/generate-order",
    "Authorization": "eyJhbGciOiJIUzUxMiJ9.eyJ1aWQiOjE4MzE2MjM3MCwiZXhwIjoxNzE2OTY1OTc3fQ.4dyAfRvrUCPXMZUyMgeJjjqcGBsveuxZw9_79qjGlbRRxRVdtBQc58zE1sVV8DyrfVnO32bLPHuF7HUh6OXWcA",
    "uid": "182893922",
    "companyId": "190",
    "scene-flow": "pro/pages/generate-order/generate-order,e74e9033d58c99ee377069c998039a2b",
    "client-version": "5.5.78",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 MicroMessenger/7.0.20.1781(0x6700143B) NetType/WIFI MiniProgramEnv/Windows WindowsWechat/WMPF WindowsWechat(0x63090a13) XWEB/9115",
    "Content-Type": "application/json",
    "xweb_xhr": "1",
    "appid": "wx059cd327295ab444",
    "sceneCode": "1007",
    "version": "3.0.0",
    "Accept": "*/*",
    "Sec-Fetch-Site": "cross-site",
    "Sec-Fetch-Mode": "cors",
    "Sec-Fetch-Dest": "empty",
    "Referer": "https://servicewechat.com/wx059cd327295ab444/1784/page-frame.html",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "zh-CN,zh;q=0.9"
}

place_all = "https://apipro.qunjielong.com/order-place/api/mina/order_place/place-all"


def run():
    try:
        data = {
            "signUpOrderDTO": {
                "userInfo": {
                    "linkMan": "林桂焕",
                    "linkPhone": "15907520574",
                    "phonePrefix": "86",
                    "province": "广东省",
                    "city": "惠州市",
                    "country": "惠阳区",
                    "linkAddress": "淡水桥背沙解地三区八巷3号裕雄粮油",
                    "userAddrId": 8300486
                },
                "proOrderItemParamList": [{
                    "actGoodId": actGoodId,
                    "buyQuantity": 1
                }],
                "dlvAddrId": 0,
                "proOrderDefExts": [],
                "markInfoJson": {},
                # "openGroups": False
                "orderDefItemList": []
            },
            "groupBuyOrderDTO": {
                "userInfo": {
                    "linkMan": "林桂焕",
                    "linkPhone": "15907520574",
                    "phonePrefix": "86",
                    "province": "广东省",
                    "city": "惠州市",
                    "country": "惠阳区",
                    "linkAddress": "淡水桥背沙解地三区八巷3号裕雄粮油",
                    "userAddrId": 8300486
                },
                "proOrderItemParamList": [{
                    "actGoodId": actGoodId,
                    "buyQuantity": 1
                }],
                "dlvAddrId": 0,
                "proOrderDefExts": [],
                "markInfoJson": {},
                "openGroups": False
                # "orderDefItemList": []
            },
            "actId": actId
        }
        res = requests.post(place_all, headers=headers, data=json.dumps(data))
        print(res.text)
        orderNo = res.json().get("data").get("orderNo")
        query_order_pay_success_toast_list = "https://apipro.qunjielong.com/order-place/api/mina/order_place/query_order_pay_success_toast_list?actId={}&orderNo={}".format(
            actId, orderNo)
        res = requests.get(query_order_pay_success_toast_list, headers=headers)
        print(res.text)
        if res.json().get("success"):
            return True
    except Exception as e:
        print(e)
        return False


with open("d.log", "w", encoding="utf-8") as f_w:
    for i in range(50):
        f_w.write(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") + "\n")
        if run():
            break
        time.sleep(0.2)

